package com.inforich.web.controller.building;

import com.inforich.building.domain.BuildingType;
import com.inforich.building.service.IBuildingTypeService;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.common.utils.poi.ExcelUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 建筑物类型Controller
 *
 * @author hjm
 * @date 2022-10-22
 */
@RestController
@RequestMapping("/building/BuildingType")
public class BuildingTypeController extends BaseController {

  @Autowired
  private IBuildingTypeService buildingTypeService;

  /**
   * 查询建筑物类型列表
   */
  @PreAuthorize("@ss.hasPermi('building:BuildingType:list')")
  @GetMapping("/list")
  public AjaxResult list(BuildingType buildingType) {
    Map<String, Object> map = beanToMap(buildingType);
    List<BuildingType> list = buildingTypeService.listBuildingType(map);
    return AjaxResult.success(list);
  }

  /**
   * 查询建筑物类型列表（排除节点）
   */
  @PreAuthorize("@ss.hasPermi('building:BuildingType:list')")
  @GetMapping("/list/exclude/{id}")
  public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id) {
    List<BuildingType> depts = buildingTypeService.listBuildingType(new HashMap<>());
    depts.removeIf(d -> d.getId().intValue() == id);
    return AjaxResult.success(depts);
  }

  /**
   * 导出建筑物类型列表
   */
  @PreAuthorize("@ss.hasPermi('building:BuildingType:export')")
  @Log(title = "建筑物类型", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, BuildingType buildingType) {
    Map<String, Object> map = beanToMap(buildingType);
    List<BuildingType> list = buildingTypeService.listBuildingType(map);
    ExcelUtil<BuildingType> util = new ExcelUtil<BuildingType>(BuildingType.class);
    util.exportExcel(response, list, "建筑物类型数据");
  }

  /**
   * 获取建筑物类型详细信息
   */
  @PreAuthorize("@ss.hasPermi('building:BuildingType:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Long id) {
    return AjaxResult.success(buildingTypeService.getById(id));
  }

  /**
   * 新增建筑物类型
   */
  @PreAuthorize("@ss.hasPermi('building:BuildingType:add')")
  @Log(title = "建筑物类型", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody BuildingType buildingType) {
    return toAjax(buildingTypeService.save(buildingType));
  }

  /**
   * 修改建筑物类型
   */
  @PreAuthorize("@ss.hasPermi('building:BuildingType:edit')")
  @Log(title = "建筑物类型", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody BuildingType buildingType) {
    return toAjax(buildingTypeService.updateById(buildingType));
  }

  /**
   * 删除建筑物类型
   */
  @PreAuthorize("@ss.hasPermi('building:BuildingType:remove')")
  @Log(title = "建筑物类型", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Long[] ids) {
    return toAjax(buildingTypeService.removeByIds(Arrays.asList(ids)));
  }

  /**
   * 树
   */
  @GetMapping("/tree")
  public AjaxResult selectBuildingTypeTreeList() {
    List<BuildingType> list = buildingTypeService.list();
    return AjaxResult.success(buildingTypeService.selectBuildingTypeTreeList(list));
  }
}
